package com.jdls.tanhua.dubbo.impl;

import cn.hutool.Hutool;
import cn.hutool.core.collection.CollUtil;
import com.jdls.tanhua.dubbo.UserLocationApi;
import com.jdls.tanhua.mongo.UserLocation;
import com.mongodb.client.result.UpdateResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @author 嘉德罗斯
 * @date 2021/11/3
 */
@DubboService
public class UserLocationImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean updateLocation(Long userId, Double longitude, Double latitude, String address) {

        Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation one = mongoTemplate.findOne(query, UserLocation.class);
        //判断当前用户位置信息是否在数据库中存在
        //如果不存在则保存
        if(one == null){
            UserLocation userLocation = new UserLocation();
            //设置位置信息
            userLocation.setAddress(address);
            //设置保存时间
            userLocation.setCreated(System.currentTimeMillis());
            //设置上一次更新时间
            userLocation.setLastUpdated(System.currentTimeMillis());
            //设置更新时间
            userLocation.setUpdated(System.currentTimeMillis());
            //设置用户id
            userLocation.setUserId(userId);
            //设置经纬度
            userLocation.setLocation(new GeoJsonPoint(longitude,latitude));
            mongoTemplate.save(userLocation);
        }else {
            //如果存在则更新数据
            Update update = Update.update("location", new GeoJsonPoint(longitude, latitude))
                    .set("updated", System.currentTimeMillis())
                    .set("lastUpdated", one.getUserId())
                    .set("address", address);

            mongoTemplate.updateFirst(query, update, UserLocation.class);
        }
        return true;
    }

    @Override
    public List<Long> search(Long userId,  String distance) {

        //查询当前用户
        Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        if(userLocation==null){
            return null;
        }
        //获取圆点
        GeoJsonPoint location = userLocation.getLocation();
        //绘制半径
        Distance distance1 = new Distance((Double.valueOf(distance)) / 1000, Metrics.KILOMETERS);

        //根据圆点和半径绘制圆
        Circle circle = new Circle(location, distance1);
        //根据圆查询
        Criteria criteria = Criteria.where("location").withinSphere(circle);
        Query location1 = Query.query(criteria);
        List<UserLocation> userLocations = mongoTemplate.find(location1, UserLocation.class);

        //获取这个范围内所有用户的id
        List<Long> list = CollUtil.getFieldValues(userLocations, "userId", Long.class);
        return list;

    }
}
